package graduation.dao.Impl;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import graduation.dao.UserDao;
import graduation.entity.User;
import graduation.vo.PageVO;
@Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
	
	@Autowired  
    public void setSessionFactoryOverride(SessionFactory sessionFactory)  
    {  
        super.setSessionFactory(sessionFactory);  
    }
	
	@Resource
	private SessionFactory sessionFactory;

	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	/**
	 * 获取全部的用户的信息
	 * 分页查询
	 */
	@Override
	public PageVO getAllUser(int page,int size) {
		// TODO Auto-generated method stub
		/*   Long count = (Long)HibernateUtil.getSession()
	                .createQuery("select count(*) from Employee")
	                .uniqueResult();
	        System.out.println(count);*/
		Session session = this.getSession();
		
		Query query = session.createQuery("from User");
		//得到滚动结果集
		ScrollableResults scroll = query.scroll();
		//滚动到最后一行
		scroll.last();
		
		int count = scroll.getRowNumber() + 1;//总行数
		//设置分页信息
		query.setFirstResult(page - 1);
		query.setMaxResults(size);
		PageVO pageMessage = new PageVO();
		pageMessage.setCountPage((count/size) + 1);
		pageMessage.setRecordsNum(count);
		pageMessage.setList(query.list());
		return pageMessage;
	}

	//	获取全部行数
	public int getCount(){
		

		 Long lo = (Long) this.getSession()
	                .createQuery("select count(*) from User")
	                .uniqueResult();
		 int count = Integer.parseInt(String.valueOf(lo)); 
	        System.out.println(count);
			return count;
	}
	/**
	 * 通过邮箱查询
	 */
	@Override
	public User findByEmail(String email) {
		// TODO Auto-generated method stub
		Session session = this.getSession();
		String hql = " from User where email=?";
		return (User) session.createQuery(hql).setParameter(0, email).uniqueResult();
	}

	/**
	 * 通过电话号码查询
	 */
	@Override
	public User findByPhone(String phone) {
		Session session = this.getSession();
		String hql = "from User where phone = ?";
		return (User) session.createQuery(hql).setParameter(0, phone).uniqueResult();
	}
	/**
	 * 通过id查询用户
	 */
	@Override
	public User getUserById(String id){
		Session session = this.getSession();
		String hql = "from User where id = ?";
		return (User) session.createQuery(hql).setParameter(0, id).uniqueResult();
	}
		
	/**
	 * 根据用户id删除
	 */
	@Override
	public void deleteUser(User user){
		Session session = this.getSession();
		session.delete(user);
	}
	/**
	 * 修改用户信息
	 */
	@Override
	public void update(User user){
		Session session = this.getSession();
		session.update(user);
	}
	
	/**
	 * 增加用户信息
	 */
	
	public void addUser(User user){
		Session session = this.getSession();
		session.save(user);
	}
	/**
	 * 通过姓名模糊查询
	 */
	@Override
	public List<User> findUserByName(String name) {
		// TODO Auto-generated method stub
		Session session = this.getSession();
		String hql = "from User u where u.name like'%"+name+"%'";
		Query query = session.createQuery(hql);
		return query.list();
	}
}
